﻿using System.Collections.Generic;
using System.Linq;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using System.ComponentModel.DataAnnotations;
using SelfService.Model;
using SelfService.Model.Models;


namespace SelfService.ViewModel.SysOrderVMs
{
    public partial class SysOrderListVm : BasePagedListVM<SysPrescriptionView, SysOrderSearcher>
    {

        protected override IEnumerable<IGridColumn<SysPrescriptionView>> InitGridHeader()
        {
            return new List<GridColumn<SysPrescriptionView>>{
                this.MakeGridHeader(x => x.CardNo),
                this.MakeGridHeader(x => x.Code),
                this.MakeGridHeader(x => x.TradeNo),
                this.MakeGridHeader(x => x.Department),
                this.MakeGridHeader(x => x.Doctor),
                this.MakeGridHeader(x => x.Amount),
                this.MakeGridHeader(x => x.AccountAmount),
                this.MakeGridHeader(x => x.FundAmount),
                this.MakeGridHeader(x => x.SelfAmount),
                this.MakeGridHeader(x => x.ReimbursementAmount),
                this.MakeGridHeader(x => x.Status),
                this.MakeGridHeader(x => x.CreateTime),
                this.MakeGridHeader(x => x.UpdateTime),
                this.MakeGridHeader(x => x.PatientName),
                this.MakeGridHeader(x => x.PayId),
                this.MakeGridHeader(x => x.ProjectList),
                this.MakeGridHeader(x => x.Projects),
                this.MakeGridHeader(x => x.SocialSecurityStatus),
                this.MakeGridHeaderAction(width: 200)
            };
        }

        public override IOrderedQueryable<SysPrescriptionView> GetSearchQuery()
        {
            var query = DC.Set<SysPrescription>()
                .CheckEqual(Searcher.PatientName, x => x.Patient.Name)
                .CheckEqual(Searcher.CardNo, x=>x.CardNo)
                .CheckEqual(Searcher.JiuZhenNo, x=>x.Code)
                .CheckEqual(Searcher.JieSuanNo, x=>x.TradeNo)
                .CheckEqual(Searcher.Department, x=>x.Department)
                .CheckEqual(Searcher.Doctor, x=>x.Doctor)
                .CheckEqual(Searcher.Status, x=>x.Status)
                .CheckEqual(Searcher.Type, x=>x.SocialSecurityStatus)
                .CheckAuth(Wtm)
                .CheckEqual(Searcher.Amount, x=>x.Amount)
                .Select(x => new SysPrescriptionView
                {
				    ID = x.ID,
                    CardNo = x.CardNo,
                    Code = x.Code,
                    TradeNo = x.TradeNo,
                    Department = x.Department,
                    Doctor = x.Doctor,
                    Amount = x.Amount,
                    Status = x.Status,
                    CreateTime = x.CreateTime,
                    UpdateTime = x.UpdateTime,
                    PatientName = x.Patient.Name,
                    PayId = x.PayId,
                    SocialSecurityStatus = x.SocialSecurityStatus,
                    AccountAmount = x.AccountAmount,
                    FundAmount = x.FundAmount,
                    ReimbursementAmount = x.ReimbursementAmount,
                    SelfAmount = x.SelfAmount,
                    ProjectList = DC.Set<SysPrescriptionProjects>()
                        .Where(e => e.PrescriptionId == x.ID)
                        .Select(e => e.Project.Name)
                        .ToList()
                })
                .OrderByDescending(x => x.UpdateTime);
            return query;
        }
    }

    public class SysPrescriptionView : SysPrescription{
        [Display(Name = "患者姓名")]
        public string PatientName { get; set; }
        
        [Display(Name = "项目清单")]
        public List<string> ProjectList { get; set; }
        
        [Display(Name = "项目清单")]
        public string Projects {
            get => ProjectList != null ? string.Join("\r\n", ProjectList) : "";
            set { }
        }
    }
}
